package com.lyh.dt.server;

import com.lyh.dt.utils.DtActiveMqUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;


public class TransactionClient {

    private static final Logger logger = LoggerFactory.getLogger(TransactionClient.class);

    public final static Map<String, DtActiveMqBean> localTransacionMap = new ConcurrentHashMap<>();

    public final static ThreadLocal<String> threadLocalGroupId = new ThreadLocal<>();

    public final static ThreadLocal<DtActiveMqBean> threadLocalDt = new ThreadLocal<>();


    /**
     * 创建事务组
     * @author liuyuhong
     * @return
     */
    public static String createTransactionGroup() {
        String groupId = UUID.randomUUID().toString();
        DtActiveMqBean bean = DtActiveMqBean.builder().groupId(groupId).commandType(1).build();
        DtActiveMqUtil.sendDtMessageToTm(bean);
        logger.info(String.format("创建事务组:%s", groupId));
        return groupId;
    }

    /**
     * 创建本地事务
     * @author liuyuhong
     * @param groupId
     * @return
     */
    public static DtActiveMqBean createTransaction(String groupId) {
        String transactionId = UUID.randomUUID().toString();
        DtActiveMqBean bean = DtActiveMqBean.builder().groupId(groupId).
                localId(transactionId).commandType(2).task(new Task()).build();
        localTransacionMap.put(groupId, bean);
        threadLocalDt.set(bean);
        threadLocalGroupId.set(groupId);
        logger.info(String.format("生成本地事务:%s", transactionId));
        return bean;
    }


    /**
     * 添加事务量
     * @author liuyuhong
     * @param groupId
     * @param groupId
     * @return
     */
    public static void addTransactionNum(String groupId) {
        DtActiveMqBean dtActiveMqBean = localTransacionMap.get(groupId);
        DtActiveMqBean bean = DtActiveMqBean.builder().groupId(groupId).commandType(3).localId(dtActiveMqBean.getLocalId()).build();
        DtActiveMqUtil.sendDtMessageToTm(bean);
        logger.info(String.format("%s向TM添加事务量", bean.getLocalId()));
    }

    /**
     * 注册事务
     * @author liuyuhong
     * @param groupId
     * @param transactionType
     * @return
     */
    public static DtActiveMqBean addTransaction(String groupId, TransactionType transactionType) {
        DtActiveMqBean dtActiveMqBean = localTransacionMap.get(groupId);
        DtActiveMqBean bean = DtActiveMqBean.builder().groupId(groupId).commandType(2).localId(dtActiveMqBean.getLocalId()).transactionType(transactionType).build();
        DtActiveMqUtil.sendDtMessageToTm(bean);
        logger.info(String.format("%s向TM注册事务，事务类型: %s", bean.getLocalId(), transactionType));
        return bean;
    }

    /**
     * 通知事务
     * @author liuyuhong
     * @param bean
     * @return
     */
    public static void informTransaction(DtActiveMqBean bean){
        if(bean.getCommandType() == 4 && StringUtils.isNotBlank(bean.getGroupId())){
            DtActiveMqBean dtActiveMqBean = localTransacionMap.get(bean.getGroupId());
            if(dtActiveMqBean != null){
                dtActiveMqBean.setTransactionType(bean.getTransactionType());
                dtActiveMqBean.getTask().signalTask();
            }
        }

    }

}
